package com.gss.armory.persistence;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.gss.armory.domain.DocAttDTO;
import com.gss.armory.domain.DocGeneralDetailsDTO;
import com.gss.armory.domain.DocumentAuthSignatoryDTO;
import com.gss.armory.domain.DocumentCustodianDetailsDTO;
import com.gss.armory.domain.DocumentLinkDTO;
import com.gss.armory.domain.DocumentMetadataValueDTO;
import com.gss.armory.domain.UserActivityGroup;
import com.gss.security.domain.UserDetailsDTO;

@Repository
public class DocumentDAO extends HibernateDaoSupport{
	
	private Logger logger = Logger.getLogger(CommonDAO.class);
	
	@Autowired
	public DocumentDAO(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
		logger.debug("inside document dao");
	}


	public DocAttDTO getDocAtt(DetachedCriteria detachedCriteria) {
		logger.debug("inside getDocAtt & detachedCriteria is=" +detachedCriteria);
		try{
		List<DocAttDTO> attDTOs= getHibernateTemplate().findByCriteria(detachedCriteria);
		if(attDTOs!=null&&attDTOs.size()>0)
		{
			return attDTOs.get(0);
		}
		else
			return null;
		}
		catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}

	public void loadDocAtrrdetail(DocAttDTO docDTO) throws Exception{
		getHibernateTemplate().load(docDTO, docDTO.getDoc_id());
	}
	
	public List<UserDetailsDTO> loadTableData(DetachedCriteria criteria) throws Exception{
		logger.debug("DAO LAYER");
		logger.debug(criteria);
		return getHibernateTemplate().findByCriteria(criteria);
	}

	public List<Long> getExpiredDoc(DetachedCriteria criteria) throws Exception {
		return getHibernateTemplate().findByCriteria(criteria);
	}


	public List<DocGeneralDetailsDTO> getDocumentDetails (
			DetachedCriteria detachedCriteria) throws Exception {
		return getHibernateTemplate().findByCriteria(detachedCriteria);
	}
	public List<DocGeneralDetailsDTO> loadDocData(DetachedCriteria docCriteria) throws Exception {
		return getHibernateTemplate().findByCriteria(docCriteria);
	}
	public void saveUserActivity(UserActivityGroup entity) throws Exception
	{
		try{
			getHibernateTemplate().save(entity);
		}
		catch(DataAccessException e) {
			logger.error(e.getMessage(),e);
		}
	}

	public List<UserActivityGroup> getUserDocumentActivity(Long docId) throws Exception {
		DetachedCriteria criteriaForActivityMapping = DetachedCriteria.forClass(UserActivityGroup.class);
		criteriaForActivityMapping.add(Restrictions.eq("docId", docId));
		return getHibernateTemplate().findByCriteria(criteriaForActivityMapping);
	}


	public void deleteUserActivity(List<UserActivityGroup> entities) throws Exception {
			getHibernateTemplate().deleteAll(entities);

		
	}
	public void saveUserActivity(List<UserActivityGroup> entities) throws Exception {
		logger.debug("*****************************size of list of users: " + entities.size());
		boolean resultOfSave = false;
		try{
			getHibernateTemplate().saveOrUpdateAll(entities);
			resultOfSave = true;
		}
		catch(Throwable e){
			logger.error(e.getMessage(),e);
		}
		logger.debug("Result Of save of activity mapping: " + resultOfSave);
	}


	public void saveDocumentLinking(List<DocumentLinkDTO> documentLinkDTOs) throws Exception {
		getHibernateTemplate().saveOrUpdateAll(documentLinkDTOs);
		
	}


	public void deleteDocumentLinking(
			List<DocumentLinkDTO> documentLinksToDelete) throws Exception {
		if(null != documentLinksToDelete && documentLinksToDelete.size() > 0)
			getHibernateTemplate().deleteAll(documentLinksToDelete);
		
	}

	public void deleteAuthSignatoryLinking(
			List<DocumentAuthSignatoryDTO> documentLinksToDelete) throws Exception {
		if(null != documentLinksToDelete && documentLinksToDelete.size() > 0)
			getHibernateTemplate().deleteAll(documentLinksToDelete);
		
	}

	public List<String> loadUserForRoleWF(DetachedCriteria detachedCriteria) throws Exception{
		
		return getHibernateTemplate().findByCriteria(detachedCriteria);
		
	}


	public DocumentCustodianDetailsDTO loadCustodianDetailforDocId(long docId) throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(DocumentCustodianDetailsDTO.class);
		criteria.add(Restrictions.eq("docId", docId));
		List<DocumentCustodianDetailsDTO> custodianDetailsList = getHibernateTemplate().findByCriteria(criteria);
		if(null != custodianDetailsList && custodianDetailsList.size() > 0)
			return custodianDetailsList.get(0);
		else
			return null;
	}


	public List<DocumentMetadataValueDTO> loadMetaDataValue(long docId) throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(DocumentMetadataValueDTO.class);
		criteria.add(Restrictions.eq("docId", docId));
		return getHibernateTemplate().findByCriteria(criteria);
		
	}


	public List loadDocIdsForAuthSignatoryId(DetachedCriteria criteria) {
		try{
			return getHibernateTemplate().findByCriteria(criteria);
		}
		catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}
	
	public List loadDocIdsForCustodianId(DetachedCriteria criteria) {
		try{
			return getHibernateTemplate().findByCriteria(criteria);
		}
		catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}
	
	public String getCustodianNameForDocId(long id) {
		String queryString = "select DCD_CUSTODIAN_NAME from DOCUMENT_CUSTODIAN_DETAILS where DCD_DOC_ID = :docId";
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		String custodianName = "";
		try {
			Query query = session.createSQLQuery(queryString);
			query.setParameter("docId", id);
			custodianName = query.list().get(0).toString();
		} 
		catch (Exception e) {
			e.printStackTrace();			
		}
		finally{
			if(session.isOpen())
				session.close();
		}
		return custodianName;
	}
}
